<template>
  <div class="main-form">
    <h4 class="text-center">当前区域实测信息</h4>
    <el-form label-width="auto" :model="statistic" label-position="left">
      <el-row :gutter="20">
        <!-- 第一行 -->
        <el-col :span="10">
          <el-form-item label="网格区域：">
            <el-input v-model="statistic.province" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="详细地址：">
            <el-input v-model="statistic.address" readonly />
          </el-form-item>
        </el-col>
        <!-- 类似地，您可以继续添加更多的行和列以适应剩余的表单项 -->
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form-item label="SO2值：">
            <el-input type="number" v-model="statistic.so2Value">
              <template #append>
                <el-tag :type="so2Level <= 2 ? 'success' : so2Level <= 4 ? 'warning' : 'danger'"
                  >{{ so2Level }}级</el-tag
                ></template
              >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item label="CO值：">
            <el-input type="number" v-model="statistic.coValue">
              <template #append
                ><el-tag :type="coLevel <= 2 ? 'success' : coLevel <= 4 ? 'warning' : 'danger'"
                  >{{ coLevel }}级</el-tag
                ></template
              >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-form-item label="SPM值：">
            <el-input type="number" v-model="statistic.spmValue">
              <template #append
                ><el-tag :type="spmLevel <= 2 ? 'success' : spmLevel <= 4 ? 'warning' : 'danger'"
                  >{{ spmLevel }}级</el-tag
                ></template
              >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="AQI等级：">
            <el-tag
              style="margin-left: 50px"
              :type="aqiId <= 2 ? 'success' : aqiId <= 4 ? 'warning' : 'danger'"
              >{{ aqiId }}级</el-tag
            >
            <el-button type="primary" plain style="margin-left: 60px" @click="drawerVisible = true"
              >AQI计算公式</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <!-- 特殊情况，如需单独占一整行的输入框 -->
        <el-col :span="10">
          <el-form-item label="实测信息：">
            <el-input v-model="statistic.information" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="备注：">
            <el-input v-model="statistic.remark" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="form-btn">
        <el-button type="primary" @click="updateStatistic(statistic)"> 提交 </el-button>

        <el-button type="info" @click="clearForm()"> 清空 </el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { useStatisticsStore } from '@/stores/statistics/statistics.js'
import { storeToRefs } from 'pinia'
import { useAqiFeedbackStore } from '@/stores/aqi/aqiFeedback.js'
import { onMounted } from 'vue'
import router from '@/router/index.js'

const statisticsStore = useStatisticsStore()
let { statistic, so2Level, coLevel, spmLevel, aqiId, drawerVisible } = storeToRefs(statisticsStore)
const aqiFeedbackStore = useAqiFeedbackStore()
const { currentAqiFeedback } = storeToRefs(aqiFeedbackStore)

const updateStatistic = () => {
  // console.log(statistic)
  const res = statisticsStore.updateStatistic()
  if (res) {
    // router.push('/home/currentTasks')
    window.location.replace('/home/currentTasks')
    statisticsStore.isShow.value = false
    statisticsStore.clear()
  }
}

const clearForm = () => {
  statisticsStore.clearForm()
}

onMounted(() => {
  if (statisticsStore.isShow !== true) {
    router.push('/home/currentTasks')
  }
  statistic.value.province = currentAqiFeedback.value.province
  statistic.value.address = currentAqiFeedback.value.address
  statistic.value.gmId = currentAqiFeedback.value.gmId
  statistic.value.fdId = currentAqiFeedback.value.memId
  statistic.value.afId = currentAqiFeedback.value.afId
})
</script>

<style scoped lang="scss">
.main-form {
  display: flex;
  flex-direction: column;
  .text-center {
    font-size: 18px;
    font-weight: bold;
    color: #6d6d70;
    margin-bottom: 10px;
    margin-top: 20px;
    text-align: center;
  }
  .el-form {
    margin-top: 10px;
    margin-left: 150px;
    .form-btn {
      display: flex;
      justify-content: space-between;
      margin-right: 250px;
      margin-left: 100px;
    }
  }
}
</style>
